package cn.usian.basicInformation.log.logTest;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Objects;

public class CustomerConvert {

    /**
     * 类型转换
     * @param obj
     * @param clazz
     * @return
     * @throws Exception
     */
    public static Object convert(Object obj, Class clazz) throws Exception {
        Object targetObj = clazz.getDeclaredConstructor().newInstance();
        Class<?> originClass = obj.getClass();
        Field[] fields = originClass.getDeclaredFields();

        for (Field field : fields) {
            if(notValidField(field) || isStaticModifier(field)) {
                continue;
            }
            field.setAccessible(true);
            Object fileAtr = field.get(obj);

            Field field1 = null;
            try {
                field1 = clazz.getDeclaredField(field.getName());
            } catch (NoSuchFieldException e) {
                continue;
            }
            if(notValidField(field1)) {
                continue;
            }
            field1.setAccessible(true);
            field1.set(targetObj,fileAtr);
        }

        return targetObj;
    }

    private static boolean notValidField(Field field) {
        return Objects.isNull(field);
    }

    private static boolean isStaticModifier(Field field) {
        return Modifier.isStatic(field.getModifiers());
    }

}

